#!/usr/bin/node

const log = console.log,
      fs = require('fs');

const delfile = (dirname, filename) => {
  let src = dirname + filename;
  try {
    if (fs.statSync(src).isFile()) {
      fs.unlinkSync(src);
      log(`${filename} 删除成功`);
      process.exit(1);
    }
    if(fs.statSync(src).isDirectory()){
      dirname = src + '/';
      let files = fs.readdirSync(src);
      for(let i = 0; i < files.length ; i++){
        let file = files[i];
        if(fs.statSync(dirname+file).isFile()){
          fs.unlinkSync(dirname+file);
          log(`${file} delete successfully`);
          continue;

        }
        if(fs.statSync(dirname+file).isDirectory()){
          delfile(dirname,file);
        }
      }
      fs.rmdirSync(dirname);
      log(`${dirname} delete successfully`);
    }
  } catch(e) {
    console.error(e.message);
    process.exit(1);
  }
}

delfile(__dirname+'/', process.argv[2]);
